<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="ordersParamsInfo.query"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search">
            </el-button> </el-input
        ></el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="ordersListData" style="width: 100%" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="200px"
        ></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger"
              >未付款</el-tag
            >
            <el-tag v-if="scope.row.pay_status === '1'" type="success"
              >已付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | format('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="ordersEditSiteBtn"
            ></el-button>
            <!-- 物流信息 -->
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="logisticsInfoBtn"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="ordersHandleSizeChange"
        @current-change="ordersHandleCurrentChange"
        :current-page="ordersParamsInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="ordersParamsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="ordersEditSiteDialogVisible"
      width="50%"
    >
      <!-- 表单区域 -->
      <el-form
        :model="ordersEditSiteDialogVisibleForm"
        :rules="ordersEditSiteDialogVisibleFormRules"
        ref="ordersEditSiteDialogVisibleFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="ordersEditSiteDialogVisibleForm.address1"
            :options="citydata"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input
            v-model="ordersEditSiteDialogVisibleForm.address2"
          ></el-input> </el-form-item
      ></el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ordersEditSiteDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 查询物流对话框 -->
    <el-dialog
      title="查询物流"
      :visible.sync="logisticsDialogVisible"
      width="50%"
    >
      <el-timeline reverse>
        <el-timeline-item
          v-for="(activity, index) in logisticsData"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  data() {
    return {
      // 获取订单数据列表参数
      ordersParamsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单数据列表总数
      total: 0,
      // 订单数据列表数据
      ordersListData: [],
      // 控制修改地址对话框
      ordersEditSiteDialogVisible: false,
      // 修改地址对话框表单数据对象
      ordersEditSiteDialogVisibleForm: {
        address1: '',
        address2: ''
      },
      // 修改地址对话框表单验证规则
      ordersEditSiteDialogVisibleFormRules: {
        address1: [{ required: true, message: '请填写区县', trigger: 'blur' }],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 级联选择器数据源
      citydata,
      // 控制查询物流对话框
      logisticsDialogVisible: false,
      // 物流信息数据
      logisticsData: []
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 获取订单数据列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.ordersParamsInfo
      })
      // 订单数据列表数据
      this.ordersListData = res.data.goods
      // 订单数据列表总数
      this.total = res.data.total
      console.log(this.ordersListData)
    },
    // pageSize 改变时会触发
    ordersHandleSizeChange(size) {
      // 最新值
      this.ordersParamsInfo.pagesize = size
      // 重新获取订单数据列表
      this.getOrdersList()
    },
    // currentPage 改变时会触发
    ordersHandleCurrentChange(page) {
      // 最新值
      this.ordersParamsInfo.pagenum = page
      // 重新获取订单数据列表
      this.getOrdersList()
    },
    // 编辑地址按钮
    ordersEditSiteBtn() {
      // 显示编辑地址对话框
      this.ordersEditSiteDialogVisible = true
    },
    // 查看物流信息Btn
    async logisticsInfoBtn() {
      // 发送请求
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      this.logisticsData = res.data
      console.log(this.logisticsData)
      // 显示查询物流对话框
      this.logisticsDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
